$(function () {
    var flag = false;
    // var defaultVal = 0;
    console.log($("#adminAdress")[0].checked);
    $(".label").on("click", function () {
        flag = !flag;
        // console.log(flag);
        // $("#adminAdress")[0].checked = flag;
        console.log($("#adminAdress")[0].checked);
        if (flag) {
            $(".label img").attr("src", "../../static/img/ico_select@3x.png");
            $(".adminAdd").css({ "color": "#1f1f1f" });
        } else {
            $(".label img").attr("src", "../../static/img/ico_selectk@3x.png");
            $(".adminAdd").css({ "color": "#a3a3a3" });
        }
    });
    //编辑地址
    var status = parseInt(localStorage.editStatus);
    var addressId = parseInt(localStorage.editAddressId);
    var regionId = parseInt(localStorage.editRegionId);
    var contact = localStorage.editContact;
    var mobile = localStorage.editMobile;
    var address = localStorage.editAddress;
    var region = localStorage.editRegion;
    // localStorage.editStatus = "";
    localStorage.editRegion = "";
    if (status === 1) {
        $("#adminAdress")[0].checked = false;
        flag = true;
        $(".label img").attr("src", "../../static/img/ico_select@3x.png");
        $(".adminAdd").css({ "color": "#1f1f1f" });
    }
    if (status === 0) {
        $("#adminAdress")[0].checked = true;
        // flag = false;
        $(".label img").attr("src", "../../static/img/ico_selectk@3x.png");
        $(".adminAdd").css({ "color": "#a3a3a3" });
    }
    if (contact) $(".catchName").val(contact);
    if (mobile) $(".tel").val(mobile);
    if (region) {
        $(".area").html(region);
    } else {
        $(".area").html(localStorage.area);
    }
    if (address) $(".address").val(address);
    //修改后保存
    $(".save").on("click", function () {
        console.log(888);
    });
    //选择地区
    var token = localStorage.token;
    localStorage.area = "";
    $(".region").on("click", function () {
        location.href = "./selectArea.html";
    });
    //添加新地址
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".save").on("click", function () {
        var contact = $(".catchName").val();
        if (!contact) {
            tips("请输入收货人");
            return false;
        }
        var mobile = $(".tel").val();
        if (!mobile) {
            tips("请输入联系电话");
            return false;
        }
        if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile))){ 
            tips("请输入正确的手机号码");
            return false; 
        } 
        var area = $(".area").html();
        if (!area) {
            tips("请选择所在地区");
            return false;
        }
        var address = $(".address").val();
        if (!address) {
            tips("请输入详细地址");
            return false;
        }
        if(address.length<5) {
            tips("详细地址不少于5个字");
            return false;
        }
        //获取选择地区的id
        if (localStorage.areaId) {
            var tempArr = localStorage.areaId.trim().split(" ");
            localStorage.areaId = "";
            regionId = parseInt(tempArr[tempArr.length - 1]);
            console.log(666);
        }
        var defaultVal = flag;
        console.log(defaultVal);
        if (defaultVal) {
            defaultVal = 1;
        } else {
            defaultVal = 0;
        }
        console.log(defaultVal);
        $.ajax({
            type: "post",
            url: domain.test + "/address",
            data: {
                id: addressId || "",
                contact: contact,
                mobile: mobile,
                address: address,
                regionId: regionId,
                default: defaultVal
            },
            beforeSend: function (xhr) {
                xhr.setRequestHeader("token", token);
            },
            success: function (response) {
                console.log(response);
                if (response.data.rtnCode === 200) {
                    location.href = "./address.html";
                }
            }
        });
    });
});